package com.jdym.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdym.R;
import com.jdym.back.entity.Menu;
import com.jdym.back.entity.RoleMenu;
import com.jdym.back.entity.vo.ManageRoleMenu;
import com.jdym.back.service.MenuService;
import com.jdym.back.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-10-20
 */
@RestController
@CrossOrigin
@RequestMapping("/back/menu")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    RoleMenuService roleMenuService;

    @RequestMapping(value = "/findAllMenus/{roleId}")
    public R findAllMenus(@PathVariable int roleId){
      List<Menu> menus=menuService.findYijiMenu();//从菜单表里找出所有的菜单
        System.out.println("所有权限菜单："+menus);
        List<RoleMenu> r= roleMenuService.findRoleByMenuId(roleId);//根据角色id在角色菜单表中找到的菜单id
        System.out.println("一级菜单："+r);
        return R.ok().data("menus",menus).data("roleMune",r);
    }

//    为角色分配新菜单
    @PostMapping(value = "/saveMenu/{roleId}")
    public R saveMenu(@PathVariable int roleId ,@RequestParam(value = "newMenu") String newMenu){
        System.out.println("数组菜单："+roleId+"-----"+newMenu);
        String[] arr=newMenu.split(",");
        RoleMenu roleMenu=new RoleMenu();
        roleMenu.setRoleId(roleId);
        for(int i=0;i<arr.length;i++){
            roleMenu.setMenuId(Integer.parseInt(arr[i]));
            roleMenuService.getBaseMapper().insert(roleMenu);
        }
        return R.ok();
    }
    @DeleteMapping(value = "/delMenus/{roleId}")
    public R delMenus(@PathVariable int roleId,@RequestParam(value = "newMenu") String newMenu){
        String[] arr=newMenu.split(",");
        System.out.println("要删除的数组："+arr);
        for(int i=0;i<arr.length;i++){
            roleMenuService.getBaseMapper().delete(new QueryWrapper<RoleMenu>().eq("role_id",roleId).eq("menu_id",arr[i]));
        }
        return R.ok();
    }
}


